# -- coding cp949 --
#-*- encoding: utf-8 -*-
from papyDB import *
from papyDBTable  import * 
from customUI import customUI


class timeCheck( customUI ):
    def __init__( self, date1, date2 ):
        customUI.__init__(self) 
        self.date1 = date1
        self.date2 = date2
        self.date1 = int(str(self.date1)[:-6] + str(self.date1)[5:-3] + str(self.date1)[8:])
        self.date2 = int(str(self.date2)[:-6] + str(self.date2)[5:-3] + str(self.date2)[8:])
    def comp( self ):
        if self.date1-self.date2<=0 : 
            return True         
        else : 
            return False    
        

#deptTableDic = { 'ANIM':ANIM , 'FX':FX , 'LIGHT':LIGHT , 'RENDER':RENDER , 'COMP':COMP , 'TD':TD  }

class PSS:
    def __init__(self , prjCodeName='' , seqName='' , shotName=''):        
        self.prjCodeName =  prjCodeName        
        self.seqName = seqName        
        self.shotName = shotName
        self.prjID   = False if prjCodeName=='' else PROJECT.search( 'prjID' , prjCodeName = prjCodeName )
        self.seqID   = False if seqName==''     else SEQUENCES.search( 'seqID' , prjID = self.prjID , seqName = seqName )
        self.shotID  = False if shotName==''    else SHOT.search( 'shotID' , seqID = self.seqID , shotName = shotName )
        self.shotSDate  = False if shotName==''    else SHOT.search( 'shotStartDate' , shotID = self.shotID)
        self.shotEDate  = False if shotName==''    else SHOT.search( 'shotEndDate' , shotID = self.shotID)


class PSSquery(PSS):
    def prjMaker( self, pName, pCode, pType, pStatus, pDesc ):
        if PROJECT.search( 'prjID' , prjCodeName = pCode ) != [] :            
            return
        dic = {'pName':pName, 'pCode':pCode, 'pType':pType, 'pStatus':pStatus, 'pDesc':pDesc}
        query = "insert into PROJECT(prjName,prjCodeName,prjType,prjStatus,prjDesc) values('%(pName)s','%(pCode)s','%(pType)s','%(pStatus)s','%(pDesc)s')" % dic        
        papyDB.query( query )
        self.prjID = PROJECT.search( 'prjID' , prjCodeName = pCode )
        
    def prjModifier( self, **arg ):       
        query = "update PROJECT set "        
        for x in arg:
            query += x + "='" + arg[x] + "' ,"
        query = query[:-1] + "where prjID=%s" % self.prjID      
        papyDB.query( query )
        
    def prjDelete( self ):
        query = "delete from PROJECT where prjID=%s" % self.prjID
        papyDB.query( query )   
        




    def seqMaker( self, seqName, seqDesc ):
        if self.prjID == False:            
            return 
        if SEQUENCES.search( 'seqID' , seqName = seqName , prjID = self.prjID ) != [] : 
            return
        dic = {'seqName':seqName, 'pID':self.prjID, 'seqDesc':seqDesc}
        query = "insert into SEQUENCES(seqName,prjID,seqDesc) values('%(seqName)s','%(pID)s','%(seqDesc)s' )" % dic        
        papyDB.query( query )
        self.seqID = SEQUENCES.search( 'seqID' , seqName = seqName , prjID = self.prjID )    
              
    def seqModifier( self, **arg ):       
        query = "update SEQUENCES set "        
        for x in arg:
            query += x + "='" + arg[x] + "' ,"
        query = query[:-1] + "where seqID=%s" % self.seqID      
        papyDB.query( query )
        
    def seqDelete( self ):
        query = "delete from SEQUENCES where seqID=%s" % self.seqID
        papyDB.query( query )  
        
        
        
        
                        
    def shotMaker( self, shotName, sLength, sImg, sSD, sED, sFD, sStatus, sDesc ):
        if self.seqID == False or self.seqID == [] :             
            return        
        if SHOT.search( 'shotID' , shotName = shotName , seqID = self.seqID ) != [] :            
            return
        dic = {'seqID':self.seqID, 'shotName':shotName, 'sLength':sLength, 'sImg':sImg, 'sSD':sSD, 'sED':sED, 'sFD':sFD, 'sStatus':sStatus, 'sDesc':sDesc}
        timeComp = timeCheck(sSD, sED)
        if timeComp.comp() == False :
            return
        query =  "insert into SHOT(seqID,shotName, shotLength,shotImg,shotStartDate,shotEndDate,shotFinalDate,shotStatus,shotDesc) "
        query += "values('%(seqID)s','%(shotName)s','%(sLength)s','%(sImg)s','%(sSD)s','%(sED)s','%(sFD)s','%(sStatus)s','%(sDesc)s')" % dic
        papyDB.query( query )   
        self.shotID = SHOT.search( 'shotID' , shotName = shotName , seqID = self.seqID ) 
        
    def shotModifier( self, **arg ): 
        query = "update SHOT set "        
        for x in arg:
            query += x + "='" + arg[x] + "' ,"
        query = query[:-1] + "where shotID=%s" % self.shotID      
        papyDB.query( query )  
        
    def shotDelete( self ):        
        query = "delete from SHOT where shotID=%s" % self.shotID
        papyDB.query( query )
        




    def attrMaker( self, dept, uID, status, sDate, eDate, fDate, desc ): 
        timeComp = timeCheck(sDate, eDate)
        shotSComp = timeCheck(self.shotSDate, sDate)
        shotEComp = timeCheck(eDate, self.shotEDate)
        if timeComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : timeComp" 
            return
        if shotSComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : shotSComp"
            return
        if shotEComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : shotEComp"
            return
        
        if dept == 'RENDER' : table = 'RENDER'
        elif dept == 'COMP' : table = 'COMP'
        else :                table = 'JOB_INFO'
        
        dic = {'table':table, 'status':status, 'sDate':sDate, 'eDate':eDate, 'fDate':fDate, 'desc':desc, 'sID':self.shotID, 'uID':uID}
        query = "insert into %(table)s(status,startDate,endDate,finalDate,description,shotID,userID) values('%(status)s','%(sDate)s','%(eDate)s','%(fDate)s','%(desc)s','%(sID)s','%(uID)s')" % dic
#        print query
        papyDB.query( query )
    
    def attrModifier( self, dept, ID, **arg ):
        self.sDate = deptTableDic[dept].search( 'startDate' , ID = ID )
        self.eDate = deptTableDic[dept].search( 'endDate' , ID = ID )
        if 'startDate' in arg : self.sDate = arg['startDate']
        if 'endDate' in arg   : self.eDate = arg['endDate']
        shotSComp = timeCheck(self.shotSDate, self.sDate)
        shotEComp = timeCheck(self.eDate, self.shotEDate)
        attrSComp = timeCheck(self.sDate, self.eDate)
        attrEComp = timeCheck(self.shotSDate, self.eDate)
        if shotSComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : shotSComp" 
            return
        if shotEComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : shotEComp"
            return
        if attrSComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : attrSComp"
            return
        if attrEComp.comp() == False : 
#            print "�좎쭨 踰붿쐞 �ㅻ쪟 : attrEComp"
            return
        query = "update %s set " % dept        
        for x in arg:
            query += x + "='" + arg[x] + "' ,"
        query = query[:-1] + "where ID=%s" % ID  
        papyDB.query( query )     
        
    def attrDelete( self, dept, delID ):
        dic = {'dept':dept, 'delID':delID}
        query = "delete from %(dept)s where id=%(delID)s" % dic
        papyDB.query( query )   
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            